#!/bin/bash
# 查看所有服务日志

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"
DOCKER_DIR="$PROJECT_ROOT/docker"

# 如果指定了服务名，只查看该服务
if [ -n "$1" ]; then
    case "$1" in
        localstack|ls)
            echo "📋 LocalStack 日志："
            docker logs browse-etl-localstack --tail 50 -f
            ;;
        postgres|pg|db)
            echo "📋 PostgreSQL 日志："
            docker logs browse-etl-postgres --tail 50 -f
            ;;
        event|event-processor|ep)
            echo "📋 Event Processor 日志："
            docker logs browse-etl-event-processor --tail 50 -f
            ;;
        lambda)
            echo "📋 Lambda 日志："
            "$SCRIPT_DIR/view-lambda-logs.sh"
            ;;
        *)
            echo "❌ 未知服务: $1"
            echo ""
            echo "可用服务："
            echo "  localstack, ls     - LocalStack"
            echo "  postgres, pg, db   - PostgreSQL"
            echo "  event, ep          - Event Processor"
            echo "  lambda             - Lambda函数"
            exit 1
            ;;
    esac
else
    # 查看所有服务的最新日志
    echo "=================================================="
    echo "📋 所有服务日志（最新20行）"
    echo "=================================================="
    
    echo ""
    echo "🔷 LocalStack："
    docker logs browse-etl-localstack --tail 20 2>&1 | tail -10
    
    echo ""
    echo "🔷 PostgreSQL："
    docker logs browse-etl-postgres --tail 20 2>&1 | tail -10
    
    echo ""
    echo "🔷 Event Processor："
    docker logs browse-etl-event-processor --tail 20 2>&1 | tail -10
    
    echo ""
    echo "=================================================="
    echo "💡 查看特定服务完整日志："
    echo "   ./scripts/logs.sh localstack"
    echo "   ./scripts/logs.sh postgres"
    echo "   ./scripts/logs.sh event"
    echo "   ./scripts/logs.sh lambda"
    echo "=================================================="
fi

